package com.soft.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.soft.entity.Team;

@Mapper
public interface TeamMapper extends BaseMapper<Team> {
    
    @Select("SELECT * FROM team WHERE creator_id = #{creatorId}")
    List<Team> selectByCreatorId(Integer creatorId);
    
    @Select("SELECT * FROM team WHERE team_name = #{teamName}")
    Team findByTeamName(String teamName);
    
    @Select("SELECT t.* FROM team t INNER JOIN team_member tm ON t.team_id = tm.team_id WHERE tm.user_id = #{userId} AND tm.status = 'active'")
    List<Team> selectByUserId(Integer userId);
    
    @Select("SELECT * FROM team ORDER BY create_time DESC")
    List<Team> selectAll();
    
    @Select("SELECT DISTINCT t.team_id as teamId, t.team_name as teamName, t.description, t.creator_id as creatorId, t.create_time as createTime " +
            "FROM team t " +
            "LEFT JOIN team_member tm ON t.team_id = tm.team_id " +
            "WHERE t.creator_id = #{userId} OR (tm.user_id = #{userId} AND tm.team_role = 'leader' AND tm.status = 'active') " +
            "ORDER BY t.create_time DESC")
    List<Map<String, Object>> getTeamsByLeaderOrCreator(Integer userId);
}
